package aqs;

import java.util.concurrent.TimeUnit;

public class DeadLockDemo {
    private static Object a = new Object();
    private static Object b = new Object();

    public static void main(String[] args) {
        new DeadLockDemo().dead();
    }
    private void dead(){
        Thread t1 = new Thread(() -> {
            synchronized (a){
                 try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("coming into a"+Thread.currentThread().getName());
                 synchronized (b){
                     try { TimeUnit.SECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
                     System.out.println("coming into b"+Thread.currentThread().getName());
                 }
            }
        }, "a");
        t1.start();

        Thread t2 = new Thread(() -> {
            synchronized (b){
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("coming into b"+Thread.currentThread().getName());
                synchronized (a){
                    try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                    System.out.println("coming into a"+Thread.currentThread().getName());
                }
            }
        }, "a");
        t2.start();
    }
}
